library Wave14a initializer init requires LegacySystem, LostCauseMAIN, ContainmentMAIN, MagnificentBastardsMAIN

	globals
		private unit Cerberus = null
		private unit Mundos = null
		
		private trigger MundosDeathTrigger = null
		private trigger CerberusDeathTrigger = null

		private integer MundosDeathCount = 0
		private integer CerberusDeathCount = 0
		
		private boolean CerbMundosDead = false
	endglobals

	private function MundosDeathConditions takes nothing returns boolean
		return GetTriggerUnit() == Mundos
	endfunction
	private function CerberusDeathConditions takes nothing returns boolean
		return GetTriggerUnit() == Cerberus
	endfunction
	
	private function MundosDeathActions takes nothing returns nothing
		local unit u = GetTriggerUnit()
	    local real x = GetUnitX(u)
        local real y = GetUnitY(u)

		set MundosDeathCount = MundosDeathCount + 1
		call TriggerSleepAction(5.0)
		if CerberusDeathCount == 0 and MundosDeathCount == 1 then
			// revive once if cerberus have not died atleast once yet
			call RemoveUnit(u)
			set bj_meleeNearestMineDist = x
            set bj_lastTransmissionDuration = y
            set udg_soundpath = "Abilities\\Spells\\Undead\\ReviveUndead\\ReviveUndead.wav"
            call DestroyEffect(AddSpecialEffect("Objects\\Spawnmodels\\Undead\\UndeadBlood\\UndeadBloodNecromancer.mdl",x,y ))
            call PlaySoundPath3D.execute()
            set Mundos = CreateUnit(Player(14),'U00T',x,y,0.0) //Clyde
			call PauseUnit(Mundos,true)
            call SuspendHeroXP(Mundos,true)
			call SetUnitAnimation( Mundos, "birth" )
			call TriggerSleepAction(1.5)
			call PauseUnit(Mundos,false)
		endif

		set u = null
	endfunction

	private function CerberusDeathActions takes nothing returns nothing
		local unit u = GetTriggerUnit()
	    local real x = GetUnitX(u)
        local real y = GetUnitY(u)
		local integer i  = 0
		
		set CerberusDeathCount = CerberusDeathCount + 1
		call TriggerSleepAction(3.0)
		
		if MundosDeathCount == 0 then
			// continue reviving until mundos dies once
			call RemoveUnit(u)
			set bj_meleeNearestMineDist = x
            set bj_lastTransmissionDuration = y
            set udg_soundpath = "Abilities\\Spells\\Undead\\ReviveUndead\\ReviveUndead.wav"
            call DestroyEffect(AddSpecialEffect("Objects\\Spawnmodels\\Undead\\UndeadBlood\\UndeadBloodNecromancer.mdl",x,y ))
            call PlaySoundPath3D.execute()
			
            set Cerberus = CreateUnit(Player(14),'U00S',x,y,0.0) //Bonnie
            call SuspendHeroXP(Cerberus,true)
			
			set x = GetRandomReal(-5000.0, 10000.0)
			set y = GetRandomReal(-5000.0, 10000.0)
			set i = 0
			loop
				exitwhen i > 20
				call IssuePointOrderById(Cerberus, ORDER.move, x, y)
				call UnitRemoveBuffs(Cerberus, true, true)
				call TriggerSleepAction(0.4)
				set i = i + 1
			endloop
			
			set x = GetRandomReal(-5000.0, 10000.0)
			set y = GetRandomReal(-5000.0, 10000.0)
			set i = 0
			loop
				exitwhen i > 14
				call IssuePointOrderById(Cerberus, ORDER.attack, x, y)
				call TriggerSleepAction(1.0)
				set i = i + 1
			endloop
		else
			// mundos already died once, dont revive cerberus again and go to next chapter.
			set CerbMundosDead = true
		endif
		
		set u = null
	endfunction
	
	
    private function Chapter2aClimax takes nothing returns nothing
        call FogTransition(0.03)
        call GankMarine(Player(13),'n004',2+udg_Spawn,1600,2000)
        call PolledWait(5.)
        call FogTransition(0.05)
        call RandomSpawn('u00L',4,IMinBJ(5, udg_Players)) //Blink
        call GankMarine(Player(14),'n002',udg_Spawn,1600,2000)
        call GankMarine(Player(13),'n004',udg_Spawn,1600,2000)
        if udg_Players>=6 then
            call RandomSpawn('ugar',1,2)
            call RandomSpawn('n002',1,2)
        endif
        call PolledWait(5.)
        call RandomSpawn('ugar',1,GetRandomInt(1,udg_Spawn)) //Gargoyle
        call GankMarine(Player(13),'n004',2+udg_Spawn,1600,2000)
    endfunction

    private function ErisEncounter takes nothing returns nothing
        local location l = GetUnitLoc(udg_Boss)
        local integer i
        call DisableTrigger(GetTriggeringTrigger())
        call SetNVGEnabled(false)
        call PauseUnit(udg_Boss,true)
        set udg_Lighting = true
        set udg_LZPoint = GetUnitLoc(udg_Boss)
        call BlackHawkWashOff.execute()
        set udg_soundpath = "Abilities\\Spells\\Undead\\Sleep\\SleepBirth1.wav"
        call PlaySoundPath.execute()
        call MultiboardSetTitleText( udg_SquadStatus, "Losing Signal Strength.." )
        call CinematicFilterGenericBJ( 2.0, BLEND_MODE_BLEND, "ReplaceableTextures\\CameraMasks\\White_mask.blp", 0.00, 0, 0.00, 100, 60.0, 0.00, 0.00, 40.00 )
        call TriggerSleepAction(0.5)
        call PauseUnit(udg_Boss,false)
        call RemoveLocation(l)
        set l = null
        call PolledWait(3.)
        if GetRandomInt(0,2)==1 then
            call GeneralText(5.0, "|cffcd950cMarine|r : I've got a feeling we're not in Kansas anymore." )
        else
            set i = GetRandomInt(1, udg_Players)
            call FieldChat(udg_Hero[udg_Sorted[i]], 5, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : Jesus H. Christ!" )
        endif
        call RemoveLocation(udg_LZPoint)
        call MultiboardSetTitleText( udg_SquadStatus, "Signal Lost" )
    endfunction

    private function ErisKilled takes nothing returns nothing
        local integer i = GetRandomInt(1, udg_Players)
        call DestroyTrigger(GetTriggeringTrigger())
        set udg_Lighting = false
        call MultiboardSetTitleText( udg_SquadStatus, "Signal Restored.." )
        call PolledWait(2.)
        call MultiboardSetTitleText( udg_SquadStatus, "Standby for reinforcements" )
        if getInteger(null,"NumCorpsman")>0 then
            call FieldChat(udg_Hero[udg_Sorted[i]], 5.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : Doc, I don't feel too good." )
        else
            call FieldChat(udg_Hero[udg_Sorted[i]], 5.0, udg_Pcolors[udg_Sorted[i]] + GetPlayerName(ConvertedPlayer(udg_Sorted[i])) + "|r : I'm losing my mind." )
        endif
        call SetNVGEnabled(true)
        call IntermissionRestore.execute()
    endfunction

    private function Wave14a2 takes nothing returns nothing
        local integer i=1
        local trigger t = CreateTrigger()
        local location l 
        set udg_CurrentWave = 14
        set udg_Visibility = 70
        call IntermissionRestore()
        set udg_Visibility = 71
        call IntermissionRestore()
        call GenericActions.execute()
		
        call TriggerSleepAction(0.1)
		set CerbMundosDead = false
		if  MundosDeathTrigger == null then
			set MundosDeathTrigger = CreateTrigger()
			call TriggerRegisterAnyUnitEventBJ(MundosDeathTrigger, EVENT_PLAYER_UNIT_DEATH)
			call TriggerAddCondition(MundosDeathTrigger, Condition(function MundosDeathConditions))
			call TriggerAddAction(MundosDeathTrigger, function MundosDeathActions)
			call DisableTrigger(MundosDeathTrigger)
		endif
		
		if  CerberusDeathTrigger == null then
			set CerberusDeathTrigger = CreateTrigger()
			call TriggerRegisterAnyUnitEventBJ(CerberusDeathTrigger, EVENT_PLAYER_UNIT_DEATH)
			call TriggerAddCondition(CerberusDeathTrigger, Condition(function CerberusDeathConditions))
			call TriggerAddAction(CerberusDeathTrigger, function CerberusDeathActions)
			call DisableTrigger(CerberusDeathTrigger)
		endif
		
		set CerberusDeathCount = 0
		set MundosDeathCount = 0
		call EnableTrigger(CerberusDeathTrigger)
		call EnableTrigger(MundosDeathTrigger)
		
		set l = GetRandomLocInRect(udg_PlayerSpawnBoundary)
        set Cerberus = CreateUnit(Player(14),'U00S',GetLocationX(l),GetLocationY(l),0.0) //Bonnie
        call SuspendHeroXP(Cerberus,true)
		call RemoveLocation(l)
		
        set l = GetRandomLocInRect(udg_PlayerSpawnBoundary)
        set Mundos = CreateUnit(Player(14),'U00T',GetLocationX(l),GetLocationY(l),0.0) //Clyde
        call SuspendHeroXP(Mundos,true)
		call RemoveLocation(l)
       
        //call TriggerRegisterUnitEvent(t,Cerberus,EVENT_UNIT_DEATH)
        //call TriggerRegisterUnitEvent(t,Mundos,EVENT_UNIT_DEATH)
        //call TriggerAddAction(t,function BonnieClyde)

		set i = 0 
		loop
            exitwhen CerbMundosDead and i >= 5
            call TriggerSleepAction(10.0)
			if i < 5 then
				call RandomSpawn('n003',2,udg_Players) //Immortal
				call RandomSpawn('n002',1,udg_Players) //Devourer
			endif
			set i=i+1
        endloop

		call PolledWait(10.)
		call DisableTrigger(CerberusDeathTrigger)
		call DisableTrigger(MundosDeathTrigger)
        call SetImmortalSpawnEnabled(false)
        call SetCrepitusSpawnEnabled(false)
        call SetGhoulSpawnEnabled(false)
            
        if udg_SquadXP>45000 and GetRandomInt(0,3)==1 and (udg_Players>2 or udg_SquadXP>100000) then
            call LostCauseMAIN()
        elseif GetRandomInt(0,1)==1 then
            call MagnificentBastardsMAIN()
        else
            call ContainmentMAIN()
        endif
		
		set t=null
		set l = null
    endfunction

    private function Wave14a1 takes nothing returns nothing
        local trigger t = CreateTrigger()
        local location l = GetRandomLocInRect(udg_PlayerSpawnBoundary)
        set udg_CurrentWave = 14
        set udg_Visibility = 70
        call IntermissionRestore()
        set udg_Visibility = 71
        call IntermissionRestore()
        call GenericActions.execute()
        call PolledWait(2.)
        set udg_Boss = CreateUnit(Player(14),'U007',GetLocationX(l),GetLocationY(l),0.0)
        call SuspendHeroXP(udg_Boss,true)
        call TriggerRegisterUnitEvent( t, udg_Boss, EVENT_UNIT_ATTACKED )
        call TriggerAddAction( t, function ErisEncounter )
        set t = CreateTrigger()
        call TriggerRegisterUnitEvent( t, udg_Boss, EVENT_UNIT_DEATH )
        call TriggerAddAction( t, function ErisKilled )
        call RemoveLocation(l)
        set l = null
        set t = null
        if udg_Mode==2 then
            call GeneralText(10.0, "|cfff3ad00Major Grimes|r : You'll have to take Eris out from a distance." )
        endif
        call PolledWait(30.0*udg_Pace)
        call RandomSpawn('uabo',1,GetRandomInt(0, 1)) //Flesh Composite
        call RandomSpawn('nsw1',1,GetRandomInt(1,udg_Players)) //Stalker
        call RandomSpawn('n002',1,udg_Players) //Devourer
        call RandomSpawn('u004',1,udg_Players) //Wraith
        call RandomSpawn('n00N',4,udg_Spawn) //Mass HP Zombie
        call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(14),'n00D',1+udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(14),'n004',2+udg_Spawn)
        call RandomSpawn('u005',1,GetRandomInt(1,udg_Players)) //Gargolye #2
        call GankMarine(Player(13),'n004',udg_Spawn,1600,2000)
        call PolledWait(30.0*udg_Pace)
        call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(14),'n00A',1)
        call PolledWait(30.0*udg_Pace)
        call GeneralText(5.0, "|cffcd950cMarine|r : A few more minutes.. Fox Company will be here." )
        call PolledWait(5.)
        call GeneralText(5.0, "|cffcd950cMarine #2|r : Fuck a duck. I don't know if we have a few minutes." )
        call RandomSpawn('nsw1',1,GetRandomInt(1,udg_Players)) //Stalker
        call RandomSpawn('u004',1,udg_Players) //Wraith
        call RandomSpawn('n00C',1,2) //Female
        call RandomSpawn('n00N',4,udg_Spawn) //Mass HP Zombie
        call GeneralText(5.0, "|cffcd950cMarine|r : Hold it together. We'll move out when Fox arrives. Pack your gear and leave unessentials behind." )
        call PolledWait(30.0*udg_Pace)
        call FogTransition(0.02)
        call RandomSpawn('u005',1,GetRandomInt(1,udg_Players)) //Gargolye #2
        call GankMarine(Player(13),'n004',2+udg_Spawn,1600,2000)
        call SetRevelationSpawnEnabled(false)
        call PolledWait(30.0*udg_Pace)
        call SetImmortalSpawnEnabled(false)
        call SetCrepitusSpawnEnabled(false)
        call SetGhoulSpawnEnabled(false)
        call Chapter2aClimax.execute()
        call PolledWait(35.0*udg_Pace)
		
        loop
            exitwhen GetWidgetLife(udg_Boss)<0.405
            call TriggerSleepAction(5.)
        endloop
		
		call PolledWait(10.)
 
        if udg_SquadXP>40000 and GetRandomInt(0,3)==1 and udg_Players>2 then
            call LostCauseMAIN()
        elseif GetRandomInt(0,2)!=1 then
            call MagnificentBastardsMAIN()
        else
            call ContainmentMAIN()
        endif
    endfunction

    private function init takes nothing returns nothing
        call RegisterFunction("Wave14a1", function Wave14a1)
        call RegisterFunction("Wave14a2", function Wave14a2)
        // call ExecuteRegisteredFunction("Wave14a")
    endfunction
    
endlibrary

